package cn.tedu.onlinestore.product.controller;

import cn.tedu.onlinestore.base.response.JsonResult;
import cn.tedu.onlinestore.base.utils.CacheUtil;
import cn.tedu.onlinestore.product.pojo.dto.ProductParam;
import cn.tedu.onlinestore.product.pojo.vo.ProductVO;
import cn.tedu.onlinestore.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/product/")
public class ProductController {
    @Autowired
    ProductService productService;

    @PostMapping("add")
    public JsonResult addProduct(ProductParam productParam){
        log.debug("新增商品:{}",productParam);
        productService.addProduct(productParam);
        return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult productList(ProductParam productParam){
        log.debug("商品列表:{}",productParam);
        List<ProductVO> list = productService.productList(productParam);
        return JsonResult.ok(list);
    }


    @GetMapping("list/{name}")
    public JsonResult productListByName(@PathVariable String name){
        log.debug("商品列表:{}",name);
        List<ProductVO> list = productService.productListByName(name);
        return JsonResult.ok(list);
    }

    @GetMapping("select/{id}")
    public JsonResult selectProductById(@PathVariable Long id){
        log.debug("查询单个商品:{}",id);
        ProductVO productVO = productService.selectProductById(id);
        return JsonResult.ok(productVO);
    }



    @PostMapping("delete/{id}")
    public JsonResult deleteProductById(@PathVariable Long id){
        log.debug("删除商品:{}",id);
        productService.deleteProductById(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    public JsonResult updateProduct(ProductParam productParam){
        log.debug("修改商品:{}",productParam);
        productService.updateProduct(productParam);
        return JsonResult.ok();
    }
}
